<?php

namespace Apps\Csoap\Controllers;

use AliPay\Pay;
use AliYun\OpenSearch;
use AMap\LBSCloud;
use Apps\Common\Enum\SearchDataType;
use Apps\Common\Libs\BeautyParlorSyncLocation;
use Apps\Common\Libs\SearchASync;
use Apps\Common\Models\BeautyParlor;
use Apps\Common\Models\Question;
use Apps\Common\Models\QuestionAnswer;
use Apps\Common\Models\SysAreaZipDistrictPhonecode;
use Apps\Common\Models\SysOpenCity;
use Phalcon\Mvc\Controller;
use Apps\Common\Libs\UploadPic;
class IndexController extends BaseController
{

    public function indexAction()
    {
        $this->view->disable();
        echo 'meelier.com index...';
    }

    public function testAction()
    {
        $this->view->disable();


        $sync = new BeautyParlorSyncLocation();
        for($i=0; $i < 30; $i++) {
            var_dump($sync->sync($i));
        }
        return;


        /*
        $config = $this->di->get('appConfig')->amap->lbs_cloud;
        $key = $config->key;
        $signKey = $config->sign_key;
        $tableId = $config->table_id;
        $syncLogTableName = 'beauty_parlor_amap_sync_log';

        //$key = '549d506ed90f2ba7a8e209dfbdf814f5';
        //$signKey = 'db03113aafe7ef14612c809ef8a690f3';
        //$tableId = '54bfb255e4b053daeafa61d7';

        $lbs = new LBSCloud($key, $signKey, $tableId, $syncLogTableName);
        var_dump($lbs->around(106.493225, 29.567485, 10, 1));
        //echo $lbs->getList(10, 1);
        */
        //echo $this->hashPassword('123456');

        /*
        foreach(QuestionAnswer::listByQuestionId(14, 10, 0) as $d) {
            echo $d->user_id, '<br />';
        }
        */
        echo rand(1000, 9999);

    }

    /**
     * 密码加密
     * @param string $pwd
     * @return string
     */
    public function hashPassword($pwd)
    {
        $config = $this->di->get('appConfig');
        $signKey = 'phalcon.ice.deng';
        if($config->offsetExists('application') && $config->application->offsetExists('cryptKey')) {
            $signKey = $config->application->cryptKey;
        }
        $str = is_string($pwd) ? $pwd : json_encode($pwd);
        return strtolower(md5($str . $signKey));
    }

    public function uploadAction()
    {
        $this->view->disable();
        echo 'post:', '<br />';
        var_dump($_POST);
        echo '<br />', 'files:', '<br />';
        var_dump($_FILES);
    }

    public function areaAction()
    {
        $this->view->disable();
        $province = SysAreaZipDistrictPhonecode::getProvince();

        $data = [];
        foreach($province as $p) {
            $pv = $p['value'];
            $city = SysAreaZipDistrictPhonecode::getCity($pv);
            $cityData = [];
            foreach($city as $c) {
                $cv = $c['value'];
                $district = SysAreaZipDistrictPhonecode::getDistrict($pv, $cv);
                $districtData = [];
                foreach($district as $d) {
                    $districtData[] = [$d['id'], $d['value']];
                }
                $cityData[] = [$cv, $districtData];
            }
            $data[] = [$pv, $cityData];
        }
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    public function alipayAction()
    {
        $this->view->disable();
        $json = '{"discount":"0.00","payment_type":"1","subject":"\u6d4b\u8bd5\u670d\u52a13","trade_no":"2015071400001000310056525977","buyer_email":"18623640927","gmt_create":"2015-07-14 11:42:54","notify_type":"trade_status_sync","quantity":"1","out_trade_no":"201507141141234760","seller_id":"2088021056329497","notify_time":"2015-07-14 11:46:33","body":"\u7f8e\u5bb9\u96621","trade_status":"WAIT_BUYER_PAY","is_total_fee_adjust":"Y","total_fee":"0.01","seller_email":"2763393126@qq.com","price":"0.01","buyer_id":"2088312777840311","notify_id":"94ba10828b1396052ead6066e79c18b83q","use_coupon":"N","sign_type":"RSA","sign":"ZC1NpuPTClv7lZJI+xoeYOnYbEEzcVws2cNalFK46qci0vl1Nj4VBxf6vdssTDcasNePhlQaNms1KuBpY5NVbJSXMz\/azjmUb\/h2mB4g45AMZ5b8Gl5pk5eUwdn69Gux\/z0VsCNcQp+ILxXMYnfD8nBYMnESpuZ5We\/41LGEbxQ="}';
        $params = json_decode($json, true);
        $payConfig = include APP_COMMON_PATH . "base-config/pay.php";
        $alipayConfig = $payConfig['alipay'];
        $pay = new Pay($alipayConfig);
        echo $pay->verifySignRSA($params, $params['sign']) ? 1 : 0;
    }

    public function tgmAction()
    {
        $this->view->disable();
        SearchASync::Instance()->noticeSync(1, SearchDataType::BeautyParlors);
        echo 'ok';
    }
    public function searchAction()
    {
        $this->view->disable();
        $config = $this->getDi()->get('appConfig')->aliyun->open_search;
        $searchObj = new OpenSearch($config->app_name, $config->access_key, $config->secret_key, $config->key_type, $config->host);
        $searchResult = $searchObj->search('敏感', 'type=' . SearchDataType::Question, 20, 0);
        print_r($searchResult);
    }
    /**
     * 编辑器上传文件
     * @date: 2016-1-18
     * @author: futao
     */
    public function uploadsAction(){
    	$this->view->disable();
    	$_FILES = $_POST;
    	$req = $this->request;
    	$upload = new UploadPic();
    	$upload->request = $req;
    	$cover = $upload->ueditor();
    	echo json_encode($cover);
    }
    /**
     * 编辑器上传文件
     * @date: 2016-1-18
     * @author: futao
     */
    public function uploadloalfileAction(){
    	$this->view->disable();
    	$upload = new UploadPic();
    	$cover = $upload->uploadUeditorPic(APP_ROOT_PATH.$_POST['filepath'],$_POST['filename']);
    	echo json_encode($cover);
    }
}